import Vue from 'vue'
import Route from 'vue-router'
import HelloWorld from '../components/HelloWorld'
import A from '../components/a'
import B from '../components/b'
Vue.use(Route)

//使用一个routes 来承接路径，通过meta 动态修改标题
const routes = [
    {path:'/helloworld',component:HelloWorld,meta:{title:'helloworld page'}},
    {path:'/',component:A},
    {path:'/a',component:A,meta:{title:'a page'}},
    {path:'b',compoent:B}
]

//装载如router 暴露出去
const router = new Route({
    routes
})
//添加全局路由守卫，将会在组件渲染前先行挂载
router.beforeEach((to,from,next) => {
    console.log('beforeEach',to,from)
    //守卫之前设置标题，进行兼容处理
    if(to.meta && to.meta.title){
        document.title = to.meta.title
    }else{
        document.title = '全局路由守卫演示'
    }
    //带上next() 代表继续往下走，否则会卡住不往下，与中间件一个道理
    next()
})
// router.beforeResolve((to,from,next) =>{
//     console.log('beforeResolve',to,from)
//     next()
// })
// router.afterEach((to,from) =>{
//     // console.log('afterEach',to,from)
//     // next()
// })
export default router
// const router =
// export default new Route({
//     routes
// })

